import { fetch } from './fetch'

export const getAccessToken = async () => {
  const res = await fetch({
    url: 'http://localhost:3000/api/getAccessToken'
  })
  const access_token = res.data?.data?.access_token
  if (access_token) {
    uni.setStorageSync('access_token', access_token)
  } else {
    uni.showToast({ title: '获取access_token失败' })
  }
}

export const wxLogin = () => {
  wx.login({
    success: async res => {
      console.log('res:', res.code)
      const response = await fetch({
        url: 'http://localhost:3000/api/login',
        method: 'POST',
        data: { code: res.code }
      })
      uni.setStorageSync('token', response.data.data.token)
    }
  })
}

// 接口已经回收
export const getUserInfo = async () => {
  const userInfo = await wx.getUserProfile({
    desc: '展示用户信息' // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
  })
  return userInfo
}
